import { IAddressAdd, IAddressId, IAllAddressList } from './../../typeings/address';
import { getAddressList, deleteAdress, editAddress, addAddress,getAddressInfo } from './../../api/address/index';
import { Module } from 'vuex'
import { IGlobalState } from '../index';
import { IAddress } from '@/typeings/address'
import * as Types from '../action-types'

const state: IAddress = {
	allAddressList: {
		addressList: [],
		disabledList: []
	},
	addressForm: {
		addressId: '',
		name: '',
		tel: '',
		province: "",
		city: "",
		county: "",
		areaCode: "",
		addressDetail: '',
		postalCode: "",
		isDefault: false
	}
}
const mutations = {
	//所有地址
	[Types.GET_ADDRESSLIST](state: IAddress, paylpad: IAllAddressList) {
		let {addressList,disabledList} = paylpad
		state.allAddressList = {addressList,disabledList}
	},
	//具体某个地址信息 修改
	[Types.GET_ADDRESSINFO](state: IAddress, paylpad: IAddressAdd) {
		state.addressForm = paylpad
	}
}

const actions = {
	//获取地址列表
	async [Types.GET_ADDRESSLIST]({ commit }: any) {
		let result = await getAddressList();
		commit(Types.GET_ADDRESSLIST, result)
	},
	//新增
	async [Types.ADD_ADDRESS]({ commit }: any,payload:IAddressAdd) {
		let result = await addAddress(payload);
		commit(Types.GET_ADDRESSLIST, result)
	},
	//修改
	async [Types.EDIT_ADDRESS]({ commit }: any,payload:IAddressAdd) {
		let result = await editAddress(payload);
		return result
	},
	//删除
	async [Types.DELETE_ADDRESS]({ commit }: any,payload:IAddressId){
		let result = await deleteAdress(payload);
		return result
	},
	// 单个地址
	async [Types.GET_ADDRESSINFO]({ commit }: any,payload:IAddressId){
		let params = payload
		let result = await getAddressInfo({params});
		commit(Types.GET_ADDRESSINFO, result)
	}
}
const address: Module<IAddress, IGlobalState> = {
	namespaced: true,
	state,
	mutations,
	actions
}
export default address;